package com.gr.dmbackend.service.Impl.sysadmin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gr.dmbackend.mapper.DormaddminMapper;
import com.gr.dmbackend.mapper.UserMapper;
import com.gr.dmbackend.model.Dormaddmin;
import com.gr.dmbackend.model.User;
import com.gr.dmbackend.service.sysadmin.AddDormAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AddDormAdminServiceImpl implements AddDormAdminService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DormaddminMapper dormaddminMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;    // 加密

    @Override
    public JSONObject addDormAdmin(Map<String, String> data) {

        JSONObject resp = new JSONObject();
        String workId = data.get("workId");
        String password = data.get("password");
        String name = data.get("name");
        String gender = data.get("gender");
        String encodePassword = passwordEncoder.encode(password);

        if (workId.isEmpty()) {
            resp.put("error_message", "工号不能为空");
            return resp;
        }
        if (password.isEmpty()) {
            resp.put("error_message", "密码不能为空");
            return resp;
        }
        if (name.isEmpty()) {
            resp.put("error_message", "姓名不能为空");
            return resp;
        }
        if (gender.isEmpty()) {
            resp.put("error_message", "性别不能为空");
            return resp;
        }
        if (workId.length() > 20) {
            resp.put("error_message", "工号长度超出限制");
            return resp;
        }
        if (encodePassword.length() > 255) {
            resp.put("error_message", "密码长度超出限制");
            return resp;
        }
        if (name.length() > 10) {
            resp.put("error_message", "姓名长度超出限制");
            return resp;
        }
        if (gender.length() > 10) {
            resp.put("error_message", "性别长度超出限制");
            return resp;
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", workId);
        List<User> users = userMapper.selectList(queryWrapper);
        if (!users.isEmpty()) {
            resp.put("error_message", "用户已存在");
            return resp;
        }

        String photo = "https://cdn.acwing.com/media/user/profile/photo/354935_lg_fb27421878.jpg";
        User user = new User(null, workId, encodePassword, photo, "dormAdmin");
        userMapper.insert(user);
        int userId = user.getId();
        Dormaddmin dormaddmin = new Dormaddmin();
        dormaddmin.setUserId(userId);
        dormaddmin.setWorkId(workId);
        dormaddmin.setName(name);
        dormaddmin.setGender(gender);
        dormaddminMapper.insert(dormaddmin);
        resp.put("error_message", "success");

        return resp;
    }
}
